
package com.fangcun.service.admin.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fangcun.bean.admin.user.response.UserDetailAdminResponse;
import com.fangcun.bean.admin.user.response.UserRoleResponse;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProRunTimeException;
import com.fangcun.entity.AdminUserRole;
import com.fangcun.entity.User;
import com.fangcun.mapper.AdminUserRoleMapper;
import com.fangcun.mapper.UserMapper;
import com.fangcun.service.admin.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private AdminUserRoleMapper adminUserRoleMapper;
	//查询所有用户
	@Override
	public  PageInfo<User> selectUser(PageBean<User> request) {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<User> list= userMapper.selectUser(request.getParam());
		if(list==null || list.size()==0) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}
	//
	@Override
	public UserDetailAdminResponse selectById(Long id) throws ProException {
		UserDetailAdminResponse user = userMapper.selectByAdminId(id);
		if(user ==null) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());	
		}
		return user;
	}

	@Override
	public int updateUser(User record)throws ProException {
		
		if(userMapper.updateByPrimaryKeySelective(record)<=0) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());	
		}
		return userMapper.updateByPrimaryKeySelective(record);
	}
	
	/****/
	@Override
	public void updateRole(AdminUserRole request) throws ProException{
		if(adminUserRoleMapper.updateByPrimaryKeySelective(request)==0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
	/****/
	@Override
	public PageInfo<UserRoleResponse> listUserRole(PageBean<User> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<UserRoleResponse> list = adminUserRoleMapper.selectUserRole(request.getParam());
		if(list==null || list.size()==0) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}
	@Override
	public AdminUserRole details(Long id)throws ProException {
		AdminUserRole adminUserRole=adminUserRoleMapper.selectByPrimaryKey(id);
		if(adminUserRole==null) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());	
		}
		return adminUserRole;
	}
}
